<?php
/**
 * Created by PhpStorm.
 * User: 钊鑫
 * Date: 2019/1/24
 * Time: 11:39
 */
namespace app\api\validate;

use app\api\model\Homeuser as HomeuserModel;

class Register extends Base
{
    protected $rule = [
        //老师与学生共用验证数据
        'type'          =>  'require|number',
        'username'      =>  'require|checkname',
        'sex'           =>  'require|number',
        'college'       =>  'require',
        'email'         =>  'require|email',
        'password'      =>  'require',
        'password2'     =>  'require|confirm:password',
        'number'        =>  'require|mobile|checknumber',
        'smscode'       =>  'require|checkcode',            //验证码
        'xcxcode'       =>  'require|isNotEmpty',           //小程序获取到code
        'sign'          =>  'require',                      //验证码标识
    ];
    protected $message  =   [
        'type.require'          =>  '用户身份不能为空！',
        'type.number'           =>  '用户身份输入有误！',
        'username.require'      =>  '该用户名不能为空！',
        'username.checkname'    =>  '该用户名已存在！',
        'student_id.require'    =>  '学号不能为空！',
        'student_id.number'     =>  '请输入有效的学号！',
        'sex.require'           =>  '性别不能为空！',
        'college.require'       =>  '请选择所属院系！',
        'class.require'         =>  '请选择所属专业',
        'email.require'         =>  '邮箱不能为空！',
        'email.email'           =>  '请输入有效的邮箱！',
        'password.require'      =>  '密码不能为空！',
        'password2.require'     =>  '确认密码不能为空！',
        'password2.confirm'     =>  '两次密码输入不一致！',
        'number.require'        =>  '手机号不能为空！',
        'number.checknumber'    =>  '手机号已存在，请重新输入！',
        'number.mobile'         =>  '请输入有效的手机号',
        'smscode.require'       =>  '验证码不能为空！',
        'smscode.checkcode'     =>  '验证码填写错误！',
        'xcxcode.require'       =>  '没有xcxcode不能获取Token！',
        'sign.require'          =>  '验证标识不能空！'
    ];

    // edit 验证场景定义
    public function sceneStudent()
    {
        $this->append('student_id', 'require|number')
            ->append('class', 'require|number');
    }

    //用户名验证
    protected function checkname($value){
        $result = HomeuserModel::where(['username'=>$value])->count();
        return ($result == 0) ? true : false;
    }

    //手机号验证
    protected function checknumber($value){
        $result = HomeuserModel::where(['number'=>$value])->count();
        return ($result == 0) ? true : false;
    }

    //用户名验证
    protected function checkcode($value){
        return true;
    }

}